package net.es.weathermap.topo.network;

import java.util.*;

import org.apache.log4j.Logger;

import net.es.oscars.client.improved.ConfigHelper;

/**
 * Singleton to store peer information map.
 * @author David Robertson, Evangelos Chaniotakis
 */
public class PeerInfo {

    private static PeerInfo instance;
    private static final String DEFAULT_INFO_FILE = "peers.yaml";

    private Logger log;
    private HashMap<String, HashMap<String, Object>> xfaces;
    private HashMap<String, HashMap<String, String>>  peers;

    /**
     * Constructor - private because this is a Singleton
     */
    private PeerInfo() {
        this.log = Logger.getLogger(this.getClass());
    }

    /**
     * @return the initialized PeerInfo singleton instance
     */
    public static PeerInfo getInstance() {
        if (PeerInfo.instance == null) {
            PeerInfo.instance = new PeerInfo();
            PeerInfo.instance.setPeers(DEFAULT_INFO_FILE);
        }
        return PeerInfo.instance;
    }

    public HashMap<String, HashMap<String, Object>> getXfaces() {
        return this.xfaces;
    }

    public HashMap<String, HashMap<String, String>> getPeers() {
        return this.peers;
    }

    private void setPeers(String fname) {
        ConfigHelper cfg = ConfigHelper.getInstance();
        Map yamlMap = cfg.getConfiguration(fname);
        assert yamlMap != null:  "Could not load peer configuration file from " +
            "file: ${basedir}/config/" + DEFAULT_INFO_FILE;
        this.xfaces = (HashMap<String, HashMap<String, Object>>) yamlMap.get("interfaces");;
        assert this.xfaces != null: "Configuration file does not have interfaces section";
        this.peers = (HashMap<String, HashMap<String, String>>) yamlMap.get("peers");;
        assert this.peers != null: "Configuration file does not have peers section";
    }
}
